package ui.handlers;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;

import ui.CreateCustomerScreen;
import ui.FindCustomerScreen;
import ui.PlaceOrderScreen;
import ui.ViewTransactionScreen;

import ui.KioskCustomerScreen;
import ui.CashierHomeScreen;

public class CustomerHandler extends BaseHandler implements ActionListener {

	private KioskCustomerScreen kioskCustomerScrn = null;
	private CashierHomeScreen cashierHomeScrn = null;
	private CreateCustomerScreen createCustomerScreen = null;
	private FindCustomerScreen findCustomerScreen = null;
	
	public CustomerHandler(KioskCustomerScreen kcs, CashierHomeScreen chs, CreateCustomerScreen ccs, FindCustomerScreen fcs)
	{
		kioskCustomerScrn = kcs;
		cashierHomeScrn = chs;
		createCustomerScreen = ccs;
		findCustomerScreen = fcs;
	}
	
	public CustomerHandler(KioskCustomerScreen kcs)
	{ this(kcs,null,null,null); }
	
	public CustomerHandler(CashierHomeScreen chs)
	{ this(null,chs,null,null);  }
	
	public CustomerHandler(CreateCustomerScreen ccs)
	{ this(null,null,ccs,null);  }
	
	public CustomerHandler(FindCustomerScreen fcs)
	{ this(null,null,null,fcs); }
	
	public void actionPerformed(ActionEvent e)
	{
		Object src = e.getSource();
		if (kioskCustomerScrn != null)
		{
			if (src == kioskCustomerScrn.getCreateLoginButton())
			{ new CreateCustomerScreen(); }
			else if (src == kioskCustomerScrn.getLoginButton())
			{ new FindCustomerScreen(); }
		}
		else if (cashierHomeScrn != null)
		{
			if (src == cashierHomeScrn.getCreateCustomerButton())
			{ new CreateCustomerScreen(); }
			else if(src == cashierHomeScrn.getViewTransButton())
			{ new ViewTransactionScreen(); }
			
		}
		else if (findCustomerScreen != null)
		{
			if (src == findCustomerScreen.getSubmitButton())
			{ try {
				findCustomerScreen.findCustomer();
				close(findCustomerScreen.getMainFrame());
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} }
		}
		else if (createCustomerScreen != null){
			if (e.getSource() == createCustomerScreen.getClearButton())
			{ createCustomerScreen.clear(); }
			else if (e.getSource() ==  createCustomerScreen.getSubmitButton())
			{ 
				String username = createCustomerScreen.createCustomer();
				//close createCustomerScreen
				//do new place order screen with new customer
				close(createCustomerScreen.getMainFrame());
				try {
					new PlaceOrderScreen(username, false);
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	}
}